const http = require('../../utils/http.js');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalShow:true,
    details:{}
  },

//打开活动规则弹窗
  ruleShow(){
    console.log(22)
    this.setData({
      modalShow: false
    })
  },
//关闭活动规则弹窗
  closeModal(){
    this.setData({
      modalShow: true
    })
  },

  //助力详情
  getZhuliDetail(){
    http.fetchApi('procedure_t', { p: `R2013009|${this.data.zl_id}` }).then(res=>{
      if(res.data.code==1){
        var details = res.data.results[0];
        this.setData({
          details
        })

        // console.log(dates.datetime)//数据
        var details = this.data.details;
        let that = this;
        let len = details.length;//时间数据长度

        function nowTime() {//时间函数
          // console.log(a)
         // for (var i = 0; i < len; i++) {
            var intDiff = details.jssj_datas;//获取数据中的时间戳  秒数
            // console.log(intDiff)
            var day = 0, hour = 0, minute = 0, second = 0;
            if (intDiff > 0) {//转换时间
              day = Math.floor(intDiff / (60 * 60 * 24));
              hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
              minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
              second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
              if (hour <= 9) hour = '0' + hour;
              if (minute <= 9) minute = '0' + minute;
              if (second <= 9) second = '0' + second;
              details.jssj_datas--;
              var str = `${hour}时${minute}分${second}秒`
              // console.log(str)  
            } else {
              var str = "已结束！";
              clearInterval(timer);
            }
            // console.log(str);
            details.difftime = str;//在数据中添加difftime参数名，把时间放进去
         // }
          that.setData({
            details
          })
         
        }

        nowTime();
        var timer = setInterval(nowTime, 1000);
        this.setData({
          timer
        })
  

      }
    })
  },

  zhuliFun(uid){
    http.fetchApi('procedure_t', { p: `R2013008|${uid}|${this.data.zl_id}` }).then(res => {
      console.log(res)
      if (res.data.code == 1) {
        if (res.data.results[0].codes == 1) {
          wx.showToast({
            title: '帮他助力成功',
            success: () => {
              // wx.reLaunch({
              //   url: '/pages/ShareInvitation/ShareInvitation?zl_id=' + this.data.zl_id + '&shareid=1'
              // })
              clearInterval(this.data.timer);
              this.getZhuliDetail();
            }
          })
        } else if(res.data.results[0].codes == 0){
          wx.showToast({
            title: '您已帮他助力过了',
            icon: 'none'
          })
        }else{
          wx.showToast({
            title: '助力失败!',
            icon: 'none'
          })
        }
      } else {
        wx.showToast({
          title: '服务器异常',
          icon: 'none'
        })
      }
    })
  },

  //好友助力
  helpZhuli(){

    if(app.globalData.uid){
      this.zhuliFun(app.globalData.uid);
    }else{
      // 获取用户信息
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            // 分享助力进来要重新登录获取uid
            wx.getUserInfo({
              success: res1 => {
                var that = this
                wx.login({
                  success: (res) => {
                    if (res.code) {
                      wx.request({
                        url: app.globalData.host + 'api/login',
                        method: 'get',
                        header: {
                          "Content-Type": "applciation/json"
                        },
                        data: { encryptedData: res1.encryptedData, iv: res1.iv, code: res.code },
                        success: (result) => {
                          var uid = result.data.uid;
                          if (result.data.code == 1) {
                            //uid保存全局                      
                            app.globalData.uid=uid
                            console.log(`R2013008|${uid}|${this.data.zl_id}`)
                            that.zhuliFun(uid)
                          } else {

                            http.showToast('助力失败' + '', '', 'none')

                          }

                        }
                      })


                    } else {
                      console.log('助力失败！' + res.errMsg)
                    }
                  }
                })

              }
            })
          }

        }
      })
    }
    
     
    

    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    
    console.log(options)
    this.setData({
      zl_id: options.zl_id,
      shareid: options.shareid || 0
    })
    this.getZhuliDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {

      title: '来了！老铁，助我一臂之力吧',

      desc: '校园帮帮',

      path: '/pages/ShareInvitation/ShareInvitation?zl_id=' + this.data.zl_id+'&shareid=1' // 路径，传递参数到指定页面。

    }
  }
})